/*
 * *
 * author:cgm
 * date:18-5-17 上午11:15
 * /
 */

package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.pojo.dto.trans.*;
import com.kaili.common.PageRequest;
import com.kaili.pojo.entity.HsEwbsTransferDetail;
import com.kaili.db.service.IHsBasicSeqService;
import com.kaili.db.service.IHsEwbsTransferDetailService;
import com.kaili.db.service.IHsEwbsTransferService;
import com.kaili.pojo.vo.TransCheckVo;
import com.kaili.pojo.vo.TransExportVo;
import com.kaili.pojo.vo.TransSendVo;
import com.kaili.common.exception.BaseExcept;
import com.kaili.except.ResponseStatus;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * package name:com.kaili.controller
 * author:hasee
 * date :2018/5/17
 * project:micro-service
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "中转服务", description = "中转服务API")
public class TransController {
    @Autowired
    IHsBasicSeqService seqService;
    @Autowired
    IHsEwbsTransferService transferService;
    @Autowired
    IHsEwbsTransferDetailService transferDetailService;

    @ApiOperation(value = "获取中转序号")
    @GetMapping("transfers/no")
    String setTransCode() {
        return seqService.seqByTypeWithType("ZZ");
    }

    @ApiOperation(value = "获取发货中转数据包")
    @GetMapping("transfers/send")
    Page<TransSendVo> getTransSendList(@ModelAttribute TransDto req) {
        if (req.getCur() != null && req.getPageSize() != null) {
            Map<Object,Object> param=new HashMap<>();
            param.putAll(new BeanMap(req));
            Page<TransSendVo> pager = new Page<TransSendVo>(req.getCur(), req.getPageSize());
            transferService.getTransferSendList(pager, param);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.TRANS_SEND_ERR);
    }

    @ApiOperation(value = "获取到货中转数据包")
    @GetMapping("transfers/arrive")
    Page<TransSendVo> getTransArriveList(@ModelAttribute TransDto req) {
        if (req.getCur() != null && req.getPageSize() != null) {
            Map<Object,Object> param=new HashMap<>();
            param.putAll(new BeanMap(req));
            Page<TransSendVo> pager = new Page<TransSendVo>(req.getCur(), req.getPageSize());
            transferService.getTransferArriveList(pager, param);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.TRANS_SEND_ERR);
    }

    @ApiOperation(value = "添加发货中转  通过transType 区分是发货中转0，到货中转1")
    @PostMapping("transfers")
    ResponseStatus addTransSend(@RequestBody TransAddBean transfer) {
        if (0 == transferService.addTransSend(transfer)) {
            return ResponseStatus.RESPONSE_OK;
        } else {
            throw new BaseExcept(ResponseStatus.TRANS_SEND_ADD_ERR);
        }
    }

    @ApiOperation(value = "获取中转单列表  通过transType 区分是发货中转0，到货中转1")
    @GetMapping("/transfers")
    Page<TransExportVo> getTransList(@ModelAttribute TransDto req) {
        if (req.getCur() != null && req.getPageSize() != null) {
            Map<Object,Object> param=new HashMap<>();
            param.putAll(new BeanMap(req));
            Page<TransExportVo> pager = new Page<TransExportVo>(req.getCur(), req.getPageSize());
            transferService.getTransList(pager, param);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.TRANS_SEND_ERR);
    }

    @ApiOperation(value = "修改发货中转  通过transType 区分是发货中转0，到货中转1")
    @PutMapping("transfers")
    ResponseStatus modifyTrans(@RequestBody TransModifyBean modify) {
        if (0 <= transferService.modifyTrans(modify)) {
            return ResponseStatus.RESPONSE_OK;
        } else {
            throw new BaseExcept(ResponseStatus.TRANS_SEND_ADD_ERR);
        }
    }

    @ApiOperation(value = "删除发货中转  通过transType 区分是发货中转0，到货中转1")
    @DeleteMapping("transfers/{transId}")
    ResponseStatus deleteTrans(@PathVariable("transId") String id) {
        if (0 <= transferService.deleteTrans(id)) {
            return ResponseStatus.RESPONSE_OK;
        } else {
            throw new BaseExcept(ResponseStatus.TRANS_SEND_ADD_ERR);
        }
    }

    @ApiOperation(value = "根据中转单ID，获取中转单详细信息")
    @GetMapping("transfers/{transId}")
    List<TransSendVo> getTransDetail(@PathVariable("transId") Long id) {
        Map search = new HashMap();
        search.put("optId", id);
        return transferService.getTransDetail(search);
    }

    @ApiOperation(value = "根据中转单ID，获取中转单详细bean信息")
    @GetMapping("transfers/{transId}/detail")
    List<HsEwbsTransferDetail> getTransDetailBean(@PathVariable("transId") Long id) {
        Map search = new HashMap();
        search.put("transfer_no", id);
        search.put("rd_status", 1);
        return transferDetailService.selectByMap(search);
    }

    @ApiOperation(value = "获取验收列表")
    @GetMapping("transfers/check")
    Page<TransCheckVo> getCheckList(@ModelAttribute CheckDto req) {
        if (req.getCur() != null && req.getPageSize() != null) {
            Map<Object,Object> param=new HashMap<>();
            param.putAll(new BeanMap(req));
            Page<TransCheckVo> pager = new Page<TransCheckVo>(req.getCur(), req.getPageSize());
            transferService.getTransCheckList(pager, param);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.TRANS_CHECK_ERR);
    }

    @ApiOperation(value = "确认验收")
    @PutMapping("transfers/confirm")
    ResponseStatus doConfirm(@RequestBody List<TransCheckBean> req) {
        if (0 == transferService.doConfirmCheck(req)) {
            return ResponseStatus.RESPONSE_OK;
        } else {
            throw new BaseExcept(ResponseStatus.TRANS_CHECK_CONFIRM_ERR);
        }
    }
}
